﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKCodeBase.Geometry;

namespace YJKRevitCode.RevitTopologySuite
{
	public class RevitPolygonDataBean : RevitOperationDataBean
	{
		public List<List<Curve>> LstlstHolesCurves
		{
			get
			{
				return this.m_lstlstHolesCurves;
			}
			private set
			{
				this.m_lstlstHolesCurves = value;
			}
		}

		public CurveLoop ShellCurveLoop
		{
			get
			{
				return this.m_shellCurveLoop;
			}
			private set
			{
				this.m_shellCurveLoop = value;
			}
		}

		public List<CurveLoop> LstHolesCurveLoop
		{
			get
			{
				return this.m_lstHolesCurveLoop;
			}
			private set
			{
				this.m_lstHolesCurveLoop = value;
			}
		}

		public double? Area
		{
			get
			{
				if (this.m_dThisArea == null)
				{
					HYPolygon hypolygon = null;
					if (GeometryTransformer.TryMakeHYPolygon(this, out hypolygon))
					{
						this.m_dThisArea = hypolygon.Area;
					}
				}
				return this.m_dThisArea;
			}
		}

		public RevitPolygonDataBean(CurveLoop inputShell, List<CurveLoop> inputLstHoles = null)
		{
			List<Curve> list = new List<Curve>();
			double nowZ = 0.0;
			this.ShellCurveLoop = inputShell;
			this.LstHolesCurveLoop = inputLstHoles;
			if (this.ShellCurveLoop == null)
			{
				throw new ArgumentException();
			}
			base.LstRevitCurve = new List<Curve>();
			foreach (Curve item in this.ShellCurveLoop)
			{
				base.LstRevitCurve.Add(item);
			}
			list.AddRange(base.LstRevitCurve);
			if (inputLstHoles != null && inputLstHoles.Count != 0)
			{
				this.LstlstHolesCurves = new List<List<Curve>>();
				List<Curve> list2 = null;
				foreach (CurveLoop curveLoop in inputLstHoles)
				{
					list2 = new List<Curve>();
					foreach (Curve item2 in curveLoop)
					{
						list2.Add(item2);
					}
					list.AddRange(list2);
					this.LstlstHolesCurves.Add(list2);
				}
			}
			if (RevitOperationDataBean.CheckIfCurvesZSame(list, out nowZ))
			{
				base.NowZ = nowZ;
				return;
			}
			throw new ArgumentException();
		}

		public bool TryGetCenterPoint(out XYZ centerPoint)
		{
			centerPoint = null;
			HYPolygon hypolygon = null;
			if (!GeometryTransformer.TryMakeHYPolygon(this, out hypolygon))
			{
				return false;
			}
			HYPointXYZ inputPoint = null;
			if (!hypolygon.TryGetCenterPoint(out inputPoint))
			{
				return false;
			}
			centerPoint = GeometryTransformer.TransformPoint(inputPoint, base.NowZ);
			return true;
		}

		public bool TryGetOneInnerPoint(out XYZ innerPoint)
		{
			innerPoint = null;
			HYPolygon hypolygon = null;
			if (!GeometryTransformer.TryMakeHYPolygon(this, out hypolygon))
			{
				return false;
			}
			HYPointXYZ inputPoint = null;
			if (!hypolygon.TryGetOneInnerPoint(out inputPoint))
			{
				return false;
			}
			innerPoint = GeometryTransformer.TransformPoint(inputPoint, base.NowZ);
			return true;
		}

		internal RevitPolygonDataBean(List<Curve> inputlstShellCurves, List<List<Curve>> inputlstlstHolesCurve = null)
		{
			List<Curve> list = new List<Curve>();
			double nowZ = 0.0;
			list.AddRange(inputlstShellCurves);
			base.LstRevitCurve = new List<Curve>();
			this.ShellCurveLoop = new CurveLoop();
			base.LstRevitCurve.AddRange(inputlstShellCurves);
			this.ListCurveToCurveLoop(base.LstRevitCurve, ref this.m_shellCurveLoop);
			if (inputlstlstHolesCurve != null && inputlstlstHolesCurve.Count != 0)
			{
				this.LstlstHolesCurves = new List<List<Curve>>();
				this.LstlstHolesCurves.AddRange(inputlstlstHolesCurve);
				this.LstHolesCurveLoop = new List<CurveLoop>();
				CurveLoop curveLoop = null;
				foreach (List<Curve> lstInputCurve in inputlstlstHolesCurve)
				{
					curveLoop = new CurveLoop();
					this.ListCurveToCurveLoop(lstInputCurve, ref curveLoop);
					if (curveLoop != null)
					{
						this.LstHolesCurveLoop.Add(curveLoop);
					}
				}
				if (this.LstHolesCurveLoop.Count == 0)
				{
					this.LstHolesCurveLoop = null;
				}
			}
			if (RevitOperationDataBean.CheckIfCurvesZSame(list, out nowZ))
			{
				base.NowZ = nowZ;
				return;
			}
			throw new ArgumentException();
		}

		private void ListCurveToCurveLoop(List<Curve> lstInputCurve, ref CurveLoop useCurveLoop)
		{
			foreach (Curve curve in lstInputCurve)
			{
				if (useCurveLoop == null)
				{
					break;
				}
				try
				{
					useCurveLoop.Append(curve);
				}
				catch (Exception)
				{
					useCurveLoop = null;
				}
			}
		}

		private List<List<Curve>> m_lstlstHolesCurves;

		private CurveLoop m_shellCurveLoop;

		private List<CurveLoop> m_lstHolesCurveLoop;

		private double? m_dThisArea = null;
	}
}
